<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->library('Huashang/staff');
		$this->load->library('Huashang/statistics');
		$this->load->library('Huashang/system');
	}
	function test(){
		$this->load->view('test');
	}
	//判断是否手机
	private function isMobile(){  
		$useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';  
		$useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';  	  
		function CheckSubstrs($substrs,$text){  
			foreach($substrs as $substr)  
				if(false!==strpos($text,$substr)){  
					return true;  
				}  
				return false;  
		}
		
		$mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
		$mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');  
			  
		$found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) ||  
				  CheckSubstrs($mobile_token_list,$useragent);  
			  
		if ($found_mobile){  
			return true;  
		}else{  
			return false;  
		}  
	}
	public function index(){

		$this->staff->checkLogin();
		$data['menu']=json_decode(file_get_contents('assets/data/menu.json'),true);
		$this->load->view($this->isMobile()?'h5/welcome':'welcome_message',$data);
	}
	public function login(){
		if($this->input->get('force')==1){
			exit("<script>window.location.href='".WEB."welcome/login';</script>");
		}
		if($this->session->userdata('login')) header(WEB);
		$this->load->view($this->isMobile()?'h5/login':'login',$data);
	}
	public function checklogin(){
		$data=$this->staff->login();
		exit(json_encode($data['msg']));
	}
	public function exit_sys(){
		$this->session->unset_userdata('admin_name');
		$this->session->unset_userdata('role_id');
		$this->session->unset_userdata('admin_id');
		$this->session->unset_userdata('login');
		header('Location:'.WEB);
	}
	
	
	public function dashboard(){
		$data['today']=array_pop($this->statistics->traffic(date('Ymd'),date('Ymd')));
		$data['yesterday']=array_pop($this->statistics->traffic(date('Ymd',strtotime('-1 days')),date('Ymd',strtotime('-1 days')))); 
		$this->load->view('startpage',$data);
	}
	//获取权限菜单
	public function getmenu()
	{
		$admin   =array_pop(json_decode($this->staff->curl_get(ADMINAPI.'manager/get_info?admin_id=1'),true));
		$contain = $admin['privileges']=='*' ? '*' : json_decode($admin['privileges'], true);
		$menu=array();
		$rows = json_decode(file_get_contents('static/data/public_menu.json'),true);
		if($contain=="*"){
			$menu=$rows;
		}else{
			foreach($rows as $k=>$v){
				if(array_key_exists($v['en_name'],$contain['public'])){
					foreach($v['submenu'] as $a=>$b){
						if(!array_key_exists($b['en_name'], $contain['public'][$v['en_name']])){
							unset($rows[$k]['submenu'][$a]);
						}else{
							$a = explode(',',$rows[$k]['submenu'][$a]['auth']);
							foreach($a as $t=>$o){
								if(!in_array($o,$contain['public'][$v['en_name']][$b['en_name']])) unset($a[$t]);
							}
							$rows[$k]['submenu'][$a]['auth']=implode(',',$a);
						}
					}
				}else{
					unset($rows[$k]);
				}
			}
				$menu=array_merge_recursive($menu,$rows);
		}
		echo json_encode($menu);
	}

	public function start(){
		$data['config']=array_pop($this->system->settings(false));
		$this->load->view('startpage',$data);
	}

}